package com.example.administrator.energyida.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.administrator.energyida.R;
import com.example.administrator.energyida.adapter.ItemListviewDashboardAdapter;
import com.example.administrator.energyida.beans.Rank;
import com.jaeger.library.StatusBarUtil;
import com.openxu.cview.chart.ProgressPieChart;
import com.openxu.cview.chart.barchart.BarVerticalChart;
import com.openxu.cview.chart.bean.BarBean;
import com.openxu.cview.chart.bean.ChartLable;
import com.openxu.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DashboardFragment extends Fragment {

    private ProgressPieChart dashboard_progressPieChart_COP;
    private ProgressPieChart dashboard_progressPieChart_EER;
    private TextView dashboard_text_capacity;
    private TextView dashboard_text_consume;
    private TextView dashboard_text_produce;
    private TextView dashboard_text_expenses;
    private BarVerticalChart dashboard_barVerticalChart;
    private Spinner dashboard_spinner;
    private ListView dashboard_listView;

    List<Rank> rankList = new ArrayList<>();

    private ItemListviewDashboardAdapter listviewDashboardAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        initView(view);

        setListeners();
        return view;
    }

    /**
     * 设置监听器
     */
    private void setListeners() {
        dashboard_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        setListView(0,dashboard_listView);
                        break;
                    case 1:
                        setListView(1,dashboard_listView);
                        break;
                    case 2:
                        setListView(2,dashboard_listView);
                        break;
                    case 3:
                        setListView(3,dashboard_listView);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view) {
        dashboard_progressPieChart_COP = (ProgressPieChart) view.findViewById(R.id.dashboard_progressPieChart_COP);
        dashboard_progressPieChart_EER = (ProgressPieChart) view.findViewById(R.id.dashboard_progressPieChart_EER);
        dashboard_text_capacity = (TextView) view.findViewById(R.id.dashboard_text_capacity);
        dashboard_text_consume = (TextView) view.findViewById(R.id.dashboard_text_consume);
        dashboard_text_produce = (TextView) view.findViewById(R.id.dashboard_text_produce);
        dashboard_text_expenses = (TextView) view.findViewById(R.id.dashboard_text_expenses);
        dashboard_barVerticalChart = (BarVerticalChart) view.findViewById(R.id.dashboard_barVerticalChart);
        dashboard_spinner = (Spinner) view.findViewById(R.id.dashboard_spinner);
        dashboard_listView = (ListView) view.findViewById(R.id.dashboard_listView);

        // 设置状态栏的颜色
        StatusBarUtil.setColor(getActivity(), Color.parseColor("#10cafd"), 50);

        Random random = new Random();
        int pro = random.nextInt(100);
        int pro2 = random.nextInt(100);
        String per = pro + "%";
        String per2 = pro2 + "%";

        setProgressPieChart(dashboard_progressPieChart_COP, Color.parseColor("#51FF5722"), Color.parseColor("#FF5722"), per, "能耗比", pro);
        setProgressPieChart(dashboard_progressPieChart_EER, Color.parseColor("#574CAF50"), Color.parseColor("#4CAF50"), per2, "效率比", pro2);

        dashboard_text_capacity.setText("产能：" + pro + "标煤");
        dashboard_text_consume.setText("耗能：" + 100 + "标煤");
        dashboard_text_produce.setText("产出：" + pro2 + "件");
        dashboard_text_expenses.setText("耗能：" + 100 + "标煤");

        setListView(0, dashboard_listView);

        initBarVerticalChart(dashboard_barVerticalChart);
    }

    private void setListView(int i, ListView listView) {
        rankList.clear();
        switch (i) {
            case 0:
                Rank rank = new Rank(1,"软件与大数据学院","80%");
                Rank rank2 = new Rank(2,"机电学院","70%");
                Rank rank3 = new Rank(3,"电子电气学院","60%");
                Rank rank4 = new Rank(4,"外国语学院","40%");
                Rank rank5 = new Rank(5,"艺术学院","37%");
                Rank rank6 = new Rank(6,"常州机电","33%");
                Rank rank7 = new Rank(7,"常州大学","30%");
                Rank rank8 = new Rank(8,"常州轻工","28%");
                Rank rank9 = new Rank(9,"常州工业","28%");
                Rank rank10 = new Rank(10,"常州纺织","20%");
                rankList.add(rank);
                rankList.add(rank2);
                rankList.add(rank3);
                rankList.add(rank4);
                rankList.add(rank5);
                rankList.add(rank6);
                rankList.add(rank7);
                rankList.add(rank8);
                rankList.add(rank9);
                rankList.add(rank10);
                break;
            case 1:
                Rank rank11 = new Rank(1,"常州信息职业技术学院","76%");
                Rank rank22 = new Rank(2,"机电学院","73%");
                Rank rank33 = new Rank(3,"软件与大数据学院","65%");
                Rank rank44 = new Rank(4,"艺术学院","59%");
                Rank rank55 = new Rank(5,"电子电气学院","57%");
                Rank rank66 = new Rank(6,"常州工业","55%");
                Rank rank77 = new Rank(7,"常州纺织","52%");
                Rank rank88 = new Rank(8,"常州轻工","52%");
                Rank rank99 = new Rank(9,"常州大学","50%");
                Rank rank100 = new Rank(10,"常州机电","40%");
                rankList.add(rank11);
                rankList.add(rank22);
                rankList.add(rank33);
                rankList.add(rank44);
                rankList.add(rank55);
                rankList.add(rank66);
                rankList.add(rank77);
                rankList.add(rank88);
                rankList.add(rank99);
                rankList.add(rank100);
                break;
            case 2:
                Rank rank12 = new Rank(1,"软件与大数据学院","96%");
                Rank rank23 = new Rank(2,"常州机电","90%");
                Rank rank34 = new Rank(3,"电子电气学院","88%");
                Rank rank45 = new Rank(4,"外国语学院","86%");
                Rank rank56 = new Rank(5,"常州纺织","82%");
                Rank rank67 = new Rank(6,"常州信息","78%");
                Rank rank78 = new Rank(7,"常州大学","77%");
                Rank rank89 = new Rank(8,"常州轻工","69%");
                Rank rank90 = new Rank(9,"艺术学院","62%");
                Rank rank101 = new Rank(10,"常州工业","50%");
                rankList.add(rank12);
                rankList.add(rank23);
                rankList.add(rank34);
                rankList.add(rank45);
                rankList.add(rank56);
                rankList.add(rank67);
                rankList.add(rank78);
                rankList.add(rank89);
                rankList.add(rank90);
                rankList.add(rank101);
                break;
            case 3:
                Rank rank01 = new Rank(1,"软件与大数据学院","70%");
                Rank rank202 = new Rank(2,"机电学院","68%");
                Rank rank303 = new Rank(3,"常州纺织","68%");
                Rank rank404 = new Rank(4,"常州轻工","66%");
                Rank rank505 = new Rank(5,"常州信息职业技术学院","60%");
                Rank rank606 = new Rank(6,"电子电气学院","55%");
                Rank rank707 = new Rank(7,"常州大学","53%");
                Rank rank808 = new Rank(8,"外国语学院","50%");
                Rank rank909 = new Rank(9,"艺术学院","40%");
                Rank rank1010 = new Rank(10,"常州机电","29%");
                rankList.add(rank01);
                rankList.add(rank202);
                rankList.add(rank303);
                rankList.add(rank404);
                rankList.add(rank505);
                rankList.add(rank606);
                rankList.add(rank707);
                rankList.add(rank808);
                rankList.add(rank909);
                rankList.add(rank1010);
                break;
            default:
                break;
        }

        listviewDashboardAdapter = new ItemListviewDashboardAdapter(getContext(),rankList);
        listView.setAdapter(listviewDashboardAdapter);
    }

    /**
     * 设置ProgressPieChart数据
     *
     * @param chart1
     * @param defColor
     * @param proColor
     * @param per
     * @param lable
     * @param progress
     */
    private void setProgressPieChart(ProgressPieChart chart1, int defColor, int proColor, String per, String lable, int progress) {
        chart1.setProSize(DensityUtil.dip2px(getContext(), 10));  //圆环宽度
        chart1.setDefColor(defColor);
        chart1.setDebug(false);
        chart1.setLoading(false);
        chart1.setProColor(proColor);  //进度颜色
        //环形中间显示的lable
        List<ChartLable> lables = new ArrayList<>();
        lables.add(new ChartLable(per,
                DensityUtil.sp2px(getContext(), 18), proColor));
        lables.add(new ChartLable(lable,
                DensityUtil.sp2px(getContext(), 12), getResources().getColor(R.color.text_color_light_gray)));
        chart1.setData(100, progress, lables);
    }

    /**
     * 初始化BarVerticalChart
     *
     * @param chart1
     */
    private void initBarVerticalChart(BarVerticalChart chart1) {
        chart1.setBarSpace(DensityUtil.dip2px(getContext(), 1));  //双柱间距
        chart1.setBarItemSpace(DensityUtil.dip2px(getContext(), 20));  //柱间距
        chart1.setYMARK_NUM(10);
        chart1.setDebug(false);
        chart1.setBarNum(4);   //一组柱子数量
        chart1.setBarColor(new int[]{Color.parseColor("#FF12CAFD"), Color.parseColor("#FFF78229"), Color.parseColor("#FF66FC70"), Color.parseColor("#FFAB97D5")});
        //X轴
        List<String> strXList = new ArrayList<>();
        //柱状图数据
        List<List<BarBean>> dataList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            //此集合为柱状图上一条数据，集合中包含几个实体就是几个柱子
            List<BarBean> list = new ArrayList<>();
            Random random = new Random();
            list.add(new BarBean(random.nextInt(40), "水"));
            list.add(new BarBean(random.nextInt(50), "电"));
            list.add(new BarBean(random.nextInt(50), "气"));
            list.add(new BarBean(random.nextInt(40), "热"));
            dataList.add(list);
            strXList.add((i + 1) + "月");
        }
        chart1.setLableTopSpace(10);
        chart1.setLoading(false);
        chart1.setData(dataList, strXList);
    }
}
